package com.bestcem.xm.om.service.impl;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.componet.redis.service.RedisService;
import com.bestcem.xm.om.constant.RedisKeyConstant;
import com.bestcem.xm.om.enums.EmailConfigTypeEnum;
import com.bestcem.xm.om.service.InvoiceService;
import com.bestcem.xm.om.service.dto.EmailConfDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/10 17:36
 */
@Slf4j
@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Resource
    private RedisService redisService;

    @Override
    public ServiceResult<EmailConfDTO> listEmailConf(String type) {
        if (StringUtils.isEmpty(type)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "type is required");
        }
        EmailConfDTO emailConfDTO = new EmailConfDTO();
        String realEmails;
        String testEmails;
        if (EmailConfigTypeEnum.ORDER.getName().equals(type)) {
            realEmails = redisService.getString(RedisKeyConstant.ORDER_EMAIL_CONF_REAL);
            testEmails = redisService.getString(RedisKeyConstant.ORDER_EMAIL_CONF_TEST);
        } else if (EmailConfigTypeEnum.INVOICE.getName().equals(type)) {
            realEmails = redisService.getString(RedisKeyConstant.INVOICE_EMAIL_CONF_REAL);
            testEmails = redisService.getString(RedisKeyConstant.INVOICE_EMAIL_CONF_TEST);
        } else {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_RANGE, "type error");
        }
        emailConfDTO.setRealEmails(JSON.parseArray(realEmails, String.class));
        emailConfDTO.setTestEmails(JSON.parseArray(testEmails, String.class));
        return ServiceResult.success(emailConfDTO);
    }

}